package com.punch.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.punch.common.exception.BusinessException;
import com.punch.common.vo.user.school.AppSchoolVO;
import com.punch.user.exception.UserExceptionEnum;
import com.punch.user.mapper.SysStudentMapper;
import com.punch.user.mapper.SysUserSchoolMapper;
import com.punch.user.model.SysUserSchool;
import com.punch.user.service.SchoolService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @className: SchoolServiceImpl
 * @description: 学校实现层
 * @author: wujj
 * @date: 2020/12/05 15:32
 **/
@Service
public class SchoolServiceImpl implements SchoolService {

    @Resource
    private SysUserSchoolMapper sysUserSchoolMapper;

    @Resource
    private SysStudentMapper studentMapper;

    /**
     * 根据地区id查询学校
     *
     * @param areaId 地区id
     * @return List<AppSchoolVO>
     */
    @Override
    public List<AppSchoolVO> listSchool(Integer areaId) {
        return sysUserSchoolMapper.selectSchoolList(areaId);
    }

    /**
     * 学校年级下的班级
     *
     * @param schoolId 班级id
     * @param grade    年级
     * @return List<String>
     */
    @Override
    public List<String> listGradeClass(Long schoolId, Integer grade) {
        QueryWrapper<SysUserSchool> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserSchool::getUserId, schoolId);
        SysUserSchool sysUserSchool = sysUserSchoolMapper.selectOne(queryWrapper);
        Optional.ofNullable(sysUserSchool).orElseThrow(() -> new BusinessException(UserExceptionEnum.SCHOOL_NOT_EXIST));
        if (sysUserSchool.getAddStuType() != 0) {
            throw new BusinessException(UserExceptionEnum.SCHOOL_NOT_PROVIDE_CLASS);
        }
        return studentMapper.selectGradeClass(schoolId, grade);
    }
}
